package com.luych.toolbox.common.page;

import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;

public interface PageResult<T> {

    /**
     * result records
     *
     * @return result records，if empty then return new a empty list, don't return null
     */
    List<T> getRecords();

    /**
     * all record count without page
     *
     * @return all record count
     */
    long getTotal();

    /**
     * convert records from T to R
     *
     * @param convertor convertor
     * @param <R>       r
     * @return PageResult<R>
     */
    default <R> PageResult<R> convert(Function<T, R> convertor) {
        final PageResult<T> that = this;
        return new PageResult<R>() {
            @Override
            public List<R> getRecords() {
                return that.getRecords() == null ? null : that.getRecords().stream().map(convertor).collect(Collectors.toList());
            }

            @Override
            public long getTotal() {
                return that.getTotal();
            }
        };
    }

}
